import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../index.dart';

/// 浮动层提示
class OverlayTipUtil {

  static final OverlayTipUtil _singleton = OverlayTipUtil._();
  static OverlayTipUtil get to => _singleton;
  OverlayTipUtil._();

  // overlay 浮动层管理
  OverlayState? _overlayState;
  // overlay 遮罩层
  OverlayEntry? _shadeOverlayEntry;

  void showOverlayTip(String error) {
    _overlayState ??= Overlay.of(Get.context!);
    _shadeOverlayEntry = OverlayEntry(builder: (context) {
      final double statusBarHeight = MediaQuery.of(context).padding.top;
      return Positioned(
        left: 0.0,
        right: 0.0,
        top: statusBarHeight,
        child: Container(
          height: 30,
          color: AppColors.error.withOpacity(0.8),
          child: Center(
            child: Text(
              error,
              style: TextStyle(
                color: AppColors.onError,
              ),
            ),
          ),
        ),
      );
    });
    _overlayState?.insert(_shadeOverlayEntry!);
  }

  void hideOverlay() {
    _shadeOverlayEntry?.remove();
  }

  // 通知样式提示
  bool show({String? localKey, String? title, String? body}) {
    if (!Platform.isAndroid) return false;// 非安卓系统不展示
    if (localKey != null && Storage().getBool(localKey) == true) return false;
    _overlayState ??= Overlay.of(Get.context!);
    _shadeOverlayEntry = OverlayEntry(builder: (context) {
      final double statusBarHeight = MediaQuery.of(context).padding.top;
      return Positioned(
        left: AppSpace.page,
        right: AppSpace.page,
        top: statusBarHeight,
        child: Container(
          // height: 30,
          color: AppColors.error.withOpacity(0.8),
          padding: const EdgeInsets.symmetric(vertical: 10),
          // decoration: BoxDecoration(
          //   image: const DecorationImage(
          //     image: AssetImage(AssetsImages.promptBackgroundPng),
          //     fit: BoxFit.cover,
          //   ),
          //   borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
          // ),
          child: ListTile(
            title: Padding(
              padding: const EdgeInsets.only(bottom: 5),
              child: Text(
                title??"",
                style: const TextStyle(
                  color: Colors.black,
                ),
              ),
            ),
            subtitle: Text(
              body??"",
              maxLines: 100,
              overflow: TextOverflow.ellipsis,
              style: const TextStyle(
                color: Color(0xFFB0B7B4),
                fontSize: 14,
              ),
            ),
          ),
        ),
      );
    });
    _overlayState?.insert(_shadeOverlayEntry!);
    return true;
  }

}
